package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Set;

@RestController("DishAdminController")
@Slf4j
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result saveDish(@RequestBody DishDTO dishDTO){
        log.info("菜品内容:{}",dishDTO);
        //新增菜品以及菜品口味
        dishService.saveDishAndFlavors(dishDTO);
        String key = "dish_" + dishDTO.getCategoryId();
        clearRedis(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageDish(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品信息：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过 菜品ID 获取当前菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询单个菜品")
    public Result<DishVO> dishList(@PathVariable Long id){
        log.info("菜品ID:{}",id);
        DishVO dishVO = dishService.selectDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改后的菜品信息：{}",dishDTO);
        dishService.updateDishAndDishFlavor(dishDTO);
        clearRedis("dish_*");
        return Result.success();
    }

    /**
     * 删除菜品信息
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("需要删除的菜品ID:{}",ids);
        dishService.deleteDish(ids);
        clearRedis("dish_*");
        return Result.success();
    }

    /**
     * 起售停售菜品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售停售菜品")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("起售停售的菜品ID：{}",id);
        log.info("起售停售后的状态：{}",status);
        dishService.startOrStop(status,id);
        clearRedis("dish_*");
        return Result.success();
    }

    /**
     * 通过分类ID获取菜品信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分类查询菜品")
    public Result<List<Dish>> dishCategoryList(Long categoryId){
        log.info("分类ID:{}",categoryId);
        List<Dish> dishList = dishService.selectDishByCategoryId(categoryId);
        return Result.success(dishList);
    }

    /**
     * 清理缓存 REDIS
     * @param key
     */
    public void clearRedis(String key){
        Set keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);
    }

}
